/* 
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
 */
#include "stdafx.h"
#include "C_单值矢量插座.h"
#include "C_数值插座.h"
#include "C_物体插座.h"
#include "通用插座.h"

#include "list/list矢量插座.h"
#include "list/list数值插座.h"
#include "材质纹理插座.h"

#include "节点/节点树.h"


using namespace std;



C_Vec2插座::C_Vec2插座(wstring name) :C_插座基类(name, E_值类型::e_Type_Vec2) {
	m_Value = {};
}

C_Vec2插座::~C_Vec2插座() {
	C_插座基类::~C_插座基类();
}

void* C_Vec2插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Vec2) {
			return dynamic_cast<C_Vec3插座*>(socket)->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_F32) {
			auto& data = DEF_F32插座数据(socket, 0);
			vec2_set(m_Value, data);
		}
		else if (socket->m_Type == E_值类型::e_Type_F32_1D) {
			auto& data = *DEF_F32插座_1D数据(socket, 0);
			if (data.count) {
				m_Value = { data.ptr_userData[0], data.ptr_userData[0] };
			}
			else {
				m_Value = { 0,0 };
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3) {
			auto& data = DEF_Vec3插座数据(socket);
			m_Value = { float32(data.x), float32(data.y) };
		}
		else {
			//m_Value
		}
	}
	return &m_Value;
}

void C_Vec2插座::f_setData(void* data, uint8 线程ID) {
	m_Value = *((vec2*)data);
}

std::string C_Vec2插座::f_构建变量代码(int32 loc) {
	std::string code;

	C_插座基类* socket = f_getLinkOutSocket(loc);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Vec2) {
			m_Code = socket->f_getCode(0);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_vec2";

		auto* rt = m_pNode->f_getThis节点树();
		switch (rt->m_树类型) {
		case E_节点树类型::e_type_着色节点树: {
			switch (socket->m_Type) {
			case e_Type_Vec3:
			case e_Type_Vec4: {
				code += "vec2 " + m_Code + " = " + "vec2(" + socket->f_getCode(loc) + ");\n";
				break;
			}
			}
			break;
		}
		default:
			code += "vec2 " + m_Code + ";\n";
			switch (socket->m_Type) {
			case e_Type_Vec3: {
				code += m_Code + " = " + "vec_3to2(" + socket->f_getCode(loc) + ");\n";
				break;
			}
			case e_Type_Vec4: {
				code += m_Code + " = " + "vec_4转2(" + socket->f_getCode(loc) + ");\n";
				break;
			}
			case e_Type_iVec2: 
			case e_Type_iVec3: {
				code += m_Code + " = " + DEF_S(_Vec2)"(" + socket->f_getCode(loc) + ");\n";
				break;
			}
			case E_值类型::e_Type_Vec2_1D: {
				code += m_Code + " = " + socket->f_getCode(loc) + "->count ? " + socket->f_getCode(loc) + "->ptr_userData[0] : {0,0};\n";
				break;
			}
			case E_值类型::e_Type_Vec3_1D: {
				code += "if(" + socket->f_getCode(loc) + "->count){\n";
				code += m_Code + " = {" + socket->f_getCode(loc) + "->ptr_userData[0].x, " + socket->f_getCode(loc) + "->ptr_userData[0].y};\n";
				code += "} else {\n";
				code += m_Code + " = {0,0};\n";
				code += "}\n";
				break;
			}
			}
			break;
		}
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_Vec2插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
	case E_值类型::e_Type_Vec4:
	case E_值类型::e_Type_Vec3:
	case E_值类型::e_Type_Vec2:
	case E_值类型::e_Type_iVec2:
	case E_值类型::e_Type_iVec3:
	case E_值类型::e_Type_F32_1D:
	case E_值类型::e_Type_Vec2_1D:
	case E_值类型::e_Type_Vec3_1D:
	case E_值类型::e_插座Type_引用数据:
		return true;
	}
	return false;
}

void C_Vec2插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
	fread(&m_Value, sizeof(vec2), 1, f);
}

void C_Vec2插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);
	fwrite(&m_Value, sizeof(vec2), 1, f);
}

PyObject* C_Vec2插座::f_bl_传入数据(PyObject* bl_data) {
	m_Value.x = PyFloat_AsDouble(PyTuple_GetItem(bl_data, 0));
	m_Value.y = PyFloat_AsDouble(PyTuple_GetItem(bl_data, 1));
	return nullptr;
}

PyObject* C_Vec2插座::f_输出_bl数据(PyObject* bl_data) {
	PyObject* data = PyTuple_New(2);
	PyTuple_SetItem(data, 0, Py_BuildValue("f", m_Value.x));
	PyTuple_SetItem(data, 1, Py_BuildValue("f", m_Value.y));

	if (bl_data == (PyObject*)0xffffffff) {
		PyObject* info = PyList_New(1);
		PyList_SetItem(info, 0, data);
		data = info;
	}

	return data;
}








C_Vec3插座::C_Vec3插座(wstring name, vec3* mapVal) :C_插座基类(name, E_值类型::e_Type_Vec3) {
	if (mapVal) {
		m_Value = mapVal;
		m_映射指针 = true;
	}
	else {
		m_Value = (vec3*)malloc(sizeof(vec3));
		(*m_Value) = { 0, 0, 0 };
		m_映射指针 = false;
	}

	m_PropValue = f_alloc_Vec3Prop(m_Value, name);
	m_PropValue.m_私有 = true;
}

C_Vec3插座::~C_Vec3插座() {
	if (!m_映射指针) {
		free(m_Value);
	}
}

void C_Vec3插座::f_update(C_插座基类 * 连接到的输入节点, C_节点基类 * node) {

}

void* C_Vec3插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Vec3) {
			return dynamic_cast<C_Vec3插座*>(socket)->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3_1D) {
			auto& data = *DEF_Vec3插座_1D数据(socket);
			if (data.count) *m_Value = data.ptr_userData[0];
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3_Array1D) {
			auto& data = *DEF_Vec3_Array1D插座数据(socket);
			if(data.size()) *m_Value = data[0];
		}
		else if (socket->m_Type == E_值类型::e_Type_F32) {
			auto& data = DEF_F32插座数据(socket, 0);
			vec3_填充(m_Value, data);
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec2) {
			auto& data = DEF_Vec2插座数据(socket);
			*m_Value = {float32(data.x), float32(data.y), 0};
		}
		else if (socket->m_Type == E_值类型::e_Type_F32_Array1D) {
			auto& data = *DEF_F32_Array1D插座数据(socket, 0);
			
			m_Value->x = data[0];
			m_Value->y = data[0];
			m_Value->z = data[0];
		}
		else if (socket->m_Type == E_值类型::e_Type_I32) {
			auto data = DEF_I32插座数据(socket, 0);
			vec3_填充(m_Value, data);
		}
		else if (socket->m_Type == E_值类型::e_Type_Object) {
			auto* data = DEF_物体插座数据(socket, 0);
			*m_Value = f_ob_get全局坐标(data, 0);
		}
		else {
			//m_Value
		}
	}
	return m_Value;
}

void C_Vec3插座::f_setData(void* data, uint8 线程ID) {
	C_插座基类::f_setData(data);
	if (data) {
		*m_Value = *((vec3*)data);
	}
}

std::string C_Vec3插座::f_构建变量代码(int32 loc) {
	std::string code;

	C_插座基类* socket = f_getLinkOutSocket(loc);
	


	if (socket) {
		if (socket->m_Type == e_Type_Vec3) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_vec3";
		code = socket->f_getCode(loc);

		auto* rt = m_pNode->f_getThis节点树();
		switch (rt->m_树类型) {
		case E_节点树类型::e_type_着色节点树: {
			switch (socket->m_Type) {
			case E_值类型::e_Type_F32:
			//	code = "vec3 " + m_Code + " = vec3(" + code + ", " + code + ", " + code + ");\n";
			//	break;
			case e_Type_Vec4: {
				code = "vec3 " + m_Code + " = vec3(" + code + ");\n";
				break;
			}
			}
			break;
		}
		default:
			switch (socket->m_Type) {
			case E_值类型::e_Type_F32:
				code = "vec3 " + m_Code + " = {" + code + ", " + code + ", " + code + "};\n";
				break;
			case E_值类型::e_Type_I32:
				code = "vec3 " + m_Code + " = {(float32)" + code + ", (float32)" + code + ", (float32)" + code + "};\n";
				break;
			case e_Type_Vec4: {
				code = "vec3 " + m_Code + " = " + "vec_4转3(" + code + ", 0);\n";
				break;
			}
			case e_Type_iVec2: {
				code = "vec3 " + m_Code + " = {(float32)((" + code + ").x), (float32)((" + code + ").y), 0};\n";
				break;
			}
			case e_Type_iVec3: {
				code = "vec3 " + m_Code + " = {(float32)((" + code + ").x), (float32)((" + code + ").y), (float32)((" + code + ").z)};\n";
				break;
			}
			case E_值类型::e_Type_F32_1D: 
			case E_值类型::e_Type_Vec3_1D:
				code = "if(" + code + "->count > 0) " + m_Code + " = " + code + "->ptr_userData[0];\n";
				code += "else " + m_Code + " = {};\n";
				code = "vec3 " + m_Code + ";\n" + code;
				break;

			case E_值类型::e_Type_纹理: {
				switch (rt->m_树类型) {
				case E_节点树类型::e_type_着色节点树: {
					std::string 纹理坐标 = m_Code;
					code = "vec2 " + 纹理坐标 + ";\n";

					switch (C_节点树::G_编译优化) {
					case 0: {

						break;
					}
					default:
						break;
					}
					S_纹理* tex = DEF_纹理插座数据(socket, 0);

					if (tex->m_TexID >= 0) {
						m_Code = "texture(Ts[nonuniformEXT(" + f_整数值转字符串(tex->m_TexID) + ")], " + m_Code + ").rgb";

						code += "switch (i32_GParam.int32Ptr[" + f_整数值转字符串(tex->m_纹理坐标计算类型) + "]) {\n";
						code += "case 0: " + 纹理坐标 + " = bound_uv.xz; break; \n";
						code += "case 1: " + 纹理坐标 + " = f_uv_spherical(bound_uv, boundSize).xy; break; \n";
						code += "case 2: " + 纹理坐标 + " = f_uv_spherical(bound_uv, boundSize).yx; break; \n";
						code += "case 3: " + 纹理坐标 + " = f_uv_spherical(bound_uv, boundSize).xx; break; \n";
						code += "}\n";
					}
					else {
						m_Code = "vec3_GParam.vec3Ptr[" + f_整数值转字符串(m_GPU缓存.m_Mem.m_偏移) + "]";
					}
				}
				default:
					break;
				}

				break;
			}
			default:
				m_Code = "";
				code = "";
				break;
			}
			break;
		}
		//m_pNode->m_Code += code;
		//m_pNode->m_ReleaseCode += "f_JIT_Fun_vec3_free(" + m_Code + ");\n";
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_Vec3插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_iVec4:
		case E_值类型::e_Type_iVec3:
		case E_值类型::e_Type_iVec2:
		case E_值类型::e_Type_Vec4:
		case E_值类型::e_Type_Vec3:
		case E_值类型::e_Type_Vec3_1D:
		case E_值类型::e_Type_iVec3_1D:
		case E_值类型::e_Type_Vec3_Array1D:
		case E_值类型::e_Type_I32:
		case E_值类型::e_Type_F32:
		case E_值类型::e_Type_F32_1D:
		case E_值类型::e_Type_纹理:
		case E_值类型::e_Type_Array:
			return true;
		default:
			break;
	}
	return false;
}



void C_Vec3插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
	fread(m_Value, sizeof(vec3), 1, f);
}

void C_Vec3插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);
	fwrite(m_Value, sizeof(vec3), 1, f);
}

void C_Vec3插座::f_Copy(const C_插座基类* socket) {
	C_插座基类::f_Copy(socket);
	const C_Vec3插座* s = dynamic_cast<const C_Vec3插座*>(socket);
	if (s) {
		(*m_Value) = *s->m_Value;
	}
}

PyObject* C_Vec3插座::f_bl_传入数据(PyObject * bl_data) {
	m_Value->x = PyFloat_AsDouble(PyTuple_GetItem(bl_data, 0));
	m_Value->y = PyFloat_AsDouble(PyTuple_GetItem(bl_data, 1));
	m_Value->z = PyFloat_AsDouble(PyTuple_GetItem(bl_data, 2));
	return nullptr;
}

PyObject * C_Vec3插座::f_输出_bl数据(PyObject* bl_data) {
	PyObject* data = PyTuple_New(3);
	PyTuple_SetItem(data, 0, Py_BuildValue("f", m_Value->x));
	PyTuple_SetItem(data, 1, Py_BuildValue("f", m_Value->y));
	PyTuple_SetItem(data, 2, Py_BuildValue("f", m_Value->z));

	if (bl_data == (PyObject*)0xffffffff) {
		PyObject* info = PyList_New(1);
		PyList_SetItem(info, 0, data);
		data = info;
	}

	return data;
}










C_Vec4插座::C_Vec4插座(wstring name)
	:C_插座基类(name, E_值类型::e_Type_Vec4)
{
	m_Value = { 0,0,0,1 };
}

C_Vec4插座::~C_Vec4插座() {
	C_插座基类::~C_插座基类();
}

void* C_Vec4插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Vec4) {
			return socket->f_getData(线程ID);
		}
		else if (socket->m_Type == E_值类型::e_Type_F32) {
			float32 v = DEF_F32插座数据(socket, 0);
			m_Value.x = v;
			m_Value.y = v;
			m_Value.z = v;
			m_Value.w = v;
		}
	}
	return &m_Value;
}

void C_Vec4插座::f_setData(void* data, int64 ID) {
	m_Value = *((vec4*)data);
}

std::string C_Vec4插座::f_构建变量代码(int32 loc) {
	C_插座基类* socket = f_getLinkOutSocket(0);

	std::string code;
	if (socket) {
		if (socket->m_Type == e_Type_Vec4) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_vec4";
		
		code = "vec4 " + m_Code + ";\n";
		switch (socket->m_Type) {
		case e_Type_Vec3: {
			code += m_Code + " = " + "vec_3转4(" + socket->f_getCode(loc) + ", 0);\n";
			break;
		}
		case E_值类型::e_Type_Vec4_1D: {
			code += "if(" + socket->f_getCode(loc) + "->count > 0) " + m_Code + " = " + socket->f_getCode(loc) + "->ptr_userData[0];\n";
			code += "else " + m_Code + " = {};\n";
			break;
		}
		}
		//m_pNode->m_Code += code;
		//m_pNode->m_ReleaseCode += "f_JIT_Fun_vec3_free(" + m_Code + ");\n";
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_Vec4插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
	case E_值类型::e_Type_Vec4:
	case E_值类型::e_Type_Vec3:
	case E_值类型::e_Type_Vec4_Array1D:
	case E_值类型::e_Type_Vec4_1D:
	case E_值类型::e_Type_Array:
		return true;
	}
	return false;
}

void C_Vec4插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

	fread(&m_Value, sizeof(vec4), 1, f);
}

void C_Vec4插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	fwrite(&m_Value, sizeof(vec4), 1, f);
}

PyObject* C_Vec4插座::f_bl_传入数据(PyObject* bl_data) {
	m_Value.x = PyFloat_AsDouble(PyTuple_GetItem(bl_data, 0));
	m_Value.y = PyFloat_AsDouble(PyTuple_GetItem(bl_data, 1));
	m_Value.z = PyFloat_AsDouble(PyTuple_GetItem(bl_data, 2));
	m_Value.w = PyFloat_AsDouble(PyTuple_GetItem(bl_data, 3));
	return nullptr;
}







C_iVec2插座::C_iVec2插座(wstring name, ivec2* m) :C_插座基类(name, E_值类型::e_Type_iVec2) {
	m_默认值 = {};
	if (m) {
		m_Value = m;
	}
	else {
		m_Value = &m_默认值;
	}
}

C_iVec2插座::~C_iVec2插座() {
	C_插座基类::~C_插座基类();
}

void* C_iVec2插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_iVec2) {
			return dynamic_cast<C_iVec2插座*>(socket)->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3) {
			auto& data = DEF_Vec3插座数据(socket);
			*m_Value = { int32(data.x), int32(data.y) };
		}
	}
	return m_Value;
}

void C_iVec2插座::f_setData(void * data, int64 ID) {
	(*m_Value) = *((ivec2*)data);
}

std::string C_iVec2插座::f_构建变量代码(int32 loc) {
	std::string code;

	C_插座基类* socket = f_getLinkOutSocket(loc);
	if (socket) {
		if (socket->m_Type == e_Type_iVec2) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_vec3";
		code = socket->f_getCode(loc);

		auto* rt = m_pNode->f_getThis节点树();
		switch (rt->m_树类型) {
			case E_节点树类型::e_type_着色节点树: {
				switch (socket->m_Type) {
					case E_值类型::e_Type_F32:
					//	code = "vec3 " + m_Code + " = vec3(" + code + ", " + code + ", " + code + ");\n";
					//	break;
					case e_Type_Vec4: {
						code = "ivec2 " + m_Code + " = ivec2(" + code + ");\n";
						break;
					}
				}
				break;
			}
			default:
				switch (socket->m_Type) {
					case E_值类型::e_Type_F32:
						code = "ivec2 " + m_Code + " = {" + code + ", " + code + "};\n";
						break;
					case E_值类型::e_Type_I32:
						code = "ivec2 " + m_Code + " = {(int32)" + code + ", (int32)" + code + "};\n";
						break;
					case e_Type_iVec3:
					case e_Type_Vec2:
					case e_Type_Vec3:
					case e_Type_Vec4: {
						code = "ivec2 " + m_Code + " = {(int32)((" + code + ").x), (int32)((" + code + ").y) };\n";
						break;
					}
					case E_值类型::e_Type_I8_1D:
					case E_值类型::e_Type_I32_1D:
					case E_值类型::e_Type_F32_1D:
						code += "if(" + code + "->count > 0) " + m_Code + " = {(int32)(" + code + "->ptr_userData[0]), (int32)(" + code + "->ptr_userData[0]) };\n";
						code += "else " + m_Code + " = {};\n";

						code = "ivec2 " + m_Code + ";\n" + code;
						break;
					case E_值类型::e_Type_Vec2_1D:
					case E_值类型::e_Type_Vec3_1D:
					case E_值类型::e_Type_Vec4_1D:
						code += "if(" + code + "->count > 0) " + m_Code + " = {(int32)(" + code + "->ptr_userData[0].x), (int32)(" + code + "->ptr_userData[0].y) };\n";
						code += "else " + m_Code + " = {};\n";

						code = "ivec2 " + m_Code + ";\n" + code;
						break;
					default:
						m_Code = "";
						code = "";
						break;
				}
				break;
		}
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_iVec2插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_F32:
		case E_值类型::e_Type_I8:
		case E_值类型::e_Type_I32:
		case E_值类型::e_Type_UI32:
		case E_值类型::e_Type_Vec2:
		case E_值类型::e_Type_Vec3:
		case E_值类型::e_Type_Vec4:
		case E_值类型::e_Type_iVec3:
		case E_值类型::e_Type_iVec2:
		//case E_值类型::e_Type_Array:
			return true;
	}
	return false;
}

void C_iVec2插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

	fread(m_Value, sizeof(ivec2), 1, f);
}

void C_iVec2插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	fwrite(m_Value, sizeof(ivec2), 1, f);
}









C_uVec2插座::C_uVec2插座(wstring name) : C_插座基类(name, E_值类型::e_Type_uVec2) {
	m_Value = {};
}

C_uVec2插座::~C_uVec2插座() {
	C_插座基类::~C_插座基类();
}

void* C_uVec2插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_uVec2) {
			return socket->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3_Array1D) {
			const auto& data = *DEF_Vec3_Array1D插座数据(socket, 0);
			if (data.size()) {
				m_Value.x = data[0].x;
				m_Value.y = data[0].y;
			}
			else {
				m_Value = {};
			}
			
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3) {
			const auto& data = DEF_Vec3插座数据(socket, 0);
			m_Value.x = data.x;
			m_Value.y = data.y;
		}
		else if (socket->m_Type == E_值类型::e_Type_F32) {
			auto data = DEF_F32插座数据(socket, 0);
			m_Value.x = data;
			m_Value.y = data;
		}
		else if (socket->m_Type == E_值类型::e_Type_F32_Array1D) {
			auto& data = *DEF_F32_Array1D插座数据(socket, 0);

			m_Value.x = data[0];
			m_Value.y = data[0];
		}
		else {
			//m_Value
		}
	}
	return &m_Value;
}

void C_uVec2插座::f_setData(void* data, uint8 线程ID) {
	m_Value = *((uvec2*)data);
}

std::string C_uVec2插座::f_构建变量代码(int32 loc) {
	std::string code;

	C_插座基类* socket = f_getLinkOutSocket(0);
	if (socket) {
		if (socket->m_Type == e_Type_uVec2) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_vec3";


		switch (socket->m_Type) {
		case e_Type_Vec3: {
			code = socket->f_getCode(loc);
			code += "uvec2 " + m_Code + " = " + "vec_f3转u2(" + code, ");\n";
			break;
		}
		}
		//m_pNode->m_Code += code;
		//m_pNode->m_ReleaseCode += "f_JIT_Fun_vec3_free(" + m_Code + ");\n";
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

void C_uVec2插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
	fread(&m_Value, sizeof(uvec2), 1, f);
}

void C_uVec2插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);
	fwrite(&m_Value, sizeof(uvec2), 1, f);
}










C_iVec3插座::C_iVec3插座(wstring name) :C_插座基类(name, E_值类型::e_Type_iVec3) {
	m_默认值 = {};
	m_转换值 = {};

	m_Value = &m_默认值;
}

C_iVec3插座::~C_iVec3插座() {
	C_插座基类::~C_插座基类();
}


void * C_iVec3插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_iVec3) {
			return dynamic_cast<C_iVec3插座*>(socket)->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec2) {
			auto& val = DEF_Vec2插座数据(socket, 0);
			m_转换值.x = val.x;
			m_转换值.y = val.y;
		}
		else if (socket->m_Type == E_值类型::e_Type_iVec2) {
			auto& val = DEF_iVec2插座数据(socket, 0);
			m_转换值.x = val.x;
			m_转换值.y = val.y;
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3) {
			auto& val = DEF_Vec3插座数据(socket);
			m_转换值.x = val.x;
			m_转换值.y = val.y;
			m_转换值.z = val.z;
			//return dynamic_cast<C_iVec3插座*>(socket)->f_getData(线程ID);
		}
		else if (socket->m_Type == E_值类型::e_Type_F32) {
			auto& val = DEF_F32插座数据(socket);
			m_转换值.x = val;
			m_转换值.y = val;
			m_转换值.z = val;
			//return dynamic_cast<C_iVec3插座*>(socket)->f_getData(线程ID);
		}
		else if (socket->m_Type == E_值类型::e_Type_I32) {
			auto& val = DEF_I32插座数据(socket);
			m_转换值.x = val;
			m_转换值.y = val;
			m_转换值.z = val;
			//return dynamic_cast<C_iVec3插座*>(socket)->f_getData(线程ID);
		}
		else if (socket->m_Type == E_值类型::e_Type_UI32) {
			auto& val = DEF_UI32插座数据(socket);
			m_转换值.x = val;
			m_转换值.y = val;
			m_转换值.z = val;
			//return dynamic_cast<C_iVec3插座*>(socket)->f_getData(线程ID);
		}
		else if (socket->m_Type == E_值类型::e_Type_I8) {
			auto& val = DEF_I8插座数据(socket, 0);
			m_转换值.x = val;
			m_转换值.y = val;
			m_转换值.z = val;
			//return dynamic_cast<C_iVec3插座*>(socket)->f_getData(线程ID);
		}
		else {

		}
		m_Value = &m_转换值;
	}
	else {
		m_Value = &m_默认值;
	}
	return m_Value;
}

void C_iVec3插座::f_setData(void * data, int64 ID) {
	*m_Value = *((ivec3*)data);
}

std::string	C_iVec3插座::f_构建变量代码(int32 loc) {
	std::string code;

	C_插座基类* socket = f_getLinkOutSocket(loc);
	if (socket) {
		if (socket->m_Type == e_Type_iVec3) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_vec3";
		code = socket->f_getCode(loc);

		auto* rt = m_pNode->f_getThis节点树();
		switch (rt->m_树类型) {
			case E_节点树类型::e_type_着色节点树: {
				switch (socket->m_Type) {
					case E_值类型::e_Type_F32:
					//	code = "vec3 " + m_Code + " = vec3(" + code + ", " + code + ", " + code + ");\n";
					//	break;
					case e_Type_Vec4: {
						code = "ivec3 " + m_Code + " = ivec3(" + code + ");\n";
						break;
					}
				}
				break;
			}
			default:
				switch (socket->m_Type) {
					case E_值类型::e_Type_F32:
						code = "ivec3 " + m_Code + " = {" + code + ", " + code + ", " + code + "};\n";
						break;
					case E_值类型::e_Type_I32:
						code = "ivec3 " + m_Code + " = {(int32)" + code + ", (int32)" + code + ", (int32)" + code + "};\n";
						break;
					case e_Type_Vec3:
					case e_Type_Vec4: {
						code = "ivec3 " + m_Code + " = {(int32)((" + code + ").x), (int32)((" + code + ").y), (int32)((" + code + ").z) };\n";
						break;
					}
					case e_Type_iVec2: 
					case e_Type_Vec2: {
						code = "ivec3 " + m_Code + " = {(int32)((" + code + ").x), (int32)((" + code + ").y) };\n";
						break;
					}
					case E_值类型::e_Type_F32_1D:
						code += "if(" + code + "->count > 0) " + m_Code + " = {(int32)(" + code + "->ptr_userData[0]), (int32)(" + code + "->ptr_userData[0]), (int32)(" + code + "->ptr_userData[0])};\n";
						code += "else " + m_Code + " = {};\n";

						code = "ivec3 " + m_Code + ";\n" + code;
						break;
					case E_值类型::e_Type_Vec3_1D:
						code += "if(" + code + "->count > 0) " + m_Code + " = {(int32)(" + code + "->ptr_userData[0].x), (int32)(" + code + "->ptr_userData[0].y), (int32)(" + code + "->ptr_userData[0].z)};\n";
						code += "else " + m_Code + " = {};\n";

						code = "ivec3 " + m_Code + ";\n" + code;
						break;
					default:
						m_Code = "";
						code = "";
						break;
				}
				break;
		}
		//m_pNode->m_Code += code;
		//m_pNode->m_ReleaseCode += "f_JIT_Fun_vec3_free(" + m_Code + ");\n";
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_iVec3插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
		case E_值类型::e_Type_F32:
		case E_值类型::e_Type_I8:
		case E_值类型::e_Type_I32:
		case E_值类型::e_Type_UI32:
		case E_值类型::e_Type_Vec2:
		case E_值类型::e_Type_Vec3:
		case E_值类型::e_Type_Vec4:
		case E_值类型::e_Type_Vec4_Array1D:
		case E_值类型::e_Type_Vec3_1D:
		case E_值类型::e_Type_Vec4_1D:
		case E_值类型::e_Type_iVec2:
		case E_值类型::e_Type_iVec3:
		case E_值类型::e_Type_iVec3_1D:
		case E_值类型::e_Type_Array:
			return true;
	}
	return false;
}

void C_iVec3插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

	fread(m_Value, sizeof(ivec3), 1, f);
}

void C_iVec3插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	fwrite(m_Value, sizeof(ivec3), 1, f);
}




C_uVec3插座::C_uVec3插座(wstring name) :C_插座基类(name, E_值类型::e_Type_uVec3) {
	m_Value = {};
}

void * C_uVec3插座::f_getData(uint8 插座) {
	C_插座基类* socket = f_getLinkOutSocket(插座);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_uVec3) {
			return dynamic_cast<C_iVec3插座*>(socket)->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_iVec3) {
			const auto v = DEF_iVec3插座数据(socket);
			vec3_cpy(m_Value, v);
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3) {
			const auto v = DEF_Vec3插座数据(socket, 0);
			vec3_cpy(m_Value, v);
		}

	}
	return &m_Value;
}

void C_uVec3插座::f_setData(void* data, uint8 线程ID) {
	m_Value = *((uvec3*)data);
}

std::string C_uVec3插座::f_构建变量代码(int32 loc) {
	std::string code;

	C_插座基类* socket = f_getLinkOutSocket(0);
	if (socket) {
		if (socket->m_Type == e_Type_uVec2) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "new_val_to_uvec3";


		switch (socket->m_Type) {
		case e_Type_Vec3: {
			//code = socket->f_getCode();
			//code += "uvec2 " + m_Code + " = " + "vec_f3转u2(" + code, ");\n";
			break;
		}
		}
		//m_pNode->m_Code += code;
		//m_pNode->m_ReleaseCode += "f_JIT_Fun_vec3_free(" + m_Code + ");\n";
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

void C_uVec3插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

	fread(&m_Value, sizeof(uvec3), 1, f);
}

void C_uVec3插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	fwrite(&m_Value, sizeof(uvec3), 1, f);
}






C_uVec4插座::C_uVec4插座(wstring name)
	:C_插座基类(name, E_值类型::e_Type_uVec4)
{
}

C_uVec4插座::~C_uVec4插座() {
	C_插座基类::~C_插座基类();
}

void * C_uVec4插座::f_getData() {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_uVec4) {
			return dynamic_cast<C_uVec4插座*>(socket)->f_getData();
		}
		else if (socket->m_Type == E_值类型::e_Type_I8) {
			auto data = DEF_I8插座数据(socket, 0);
			m_Value.x = data;
			m_Value.y = data;
			m_Value.z = data;
			m_Value.w = data;
		}
		else if (socket->m_Type == E_值类型::e_Type_I32) {
			auto data = DEF_I32插座数据(socket);
			m_Value.x = data;
			m_Value.y = data;
			m_Value.z = data;
			m_Value.w = data;
		}
		else if (socket->m_Type == E_值类型::e_Type_F32) {
			auto data = DEF_F32插座数据(socket);
			m_Value.x = data;
			m_Value.y = data;
			m_Value.z = data;
			m_Value.w = data;
		}
		else if (socket->m_Type == E_值类型::e_Type_I8_1D) {
			auto& data = *DEF_I8插座_1D数据(socket, 0);
			if (data.count) {
				m_Value.x = data.ptr_userData[0];
				m_Value.y = m_Value.x;
				m_Value.z = m_Value.x;
				m_Value.w = m_Value.x;
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_F32_Array1D) {
			auto& data = *DEF_F32_Array1D插座数据(socket, 0);
			if (data.size()) {
				m_Value.x = data[0];
				m_Value.y = data[0];
				m_Value.z = data[0];
				m_Value.w = data[0];
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_I32_1D) {
			auto& data = *DEF_I32插座_1D数据(socket, 0);
			if (data.count) {
				m_Value.x = data.ptr_userData[0];
				m_Value.y = data.ptr_userData[0];
				m_Value.z = data.ptr_userData[0];
				m_Value.w = data.ptr_userData[0];
			}
		}

	}
	return &m_Value;
}

void C_uVec4插座::f_setData(void * data, int64 ID) {
	m_Value = *((uvec4*)data);
}

void C_uVec4插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

	fread(&m_Value, sizeof(uvec4), 1, f);
}

void C_uVec4插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	fwrite(&m_Value, sizeof(uvec4), 1, f);
}

PyObject * C_uVec4插座::f_bl_传入数据(PyObject * bl_data) {
	m_Value.x = PyLong_AsLong(PyTuple_GetItem(bl_data, 0));
	m_Value.y = PyLong_AsLong(PyTuple_GetItem(bl_data, 1));
	m_Value.z = PyLong_AsLong(PyTuple_GetItem(bl_data, 2));
	m_Value.w = PyLong_AsLong(PyTuple_GetItem(bl_data, 3));
	return nullptr;
}














C_Mat3X3插座::C_Mat3X3插座(wstring name)
	:C_插座基类(name, E_值类型::e_Type_Mat3X3)
{
}

C_Mat3X3插座::~C_Mat3X3插座() {
	C_插座基类::~C_插座基类();
}

void* C_Mat3X3插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Mat3X3) {
			return dynamic_cast<C_Mat3X3插座*>(socket)->f_getData(线程ID);
		}
	}
	return &m_Value;
}

void C_Mat3X3插座::f_setData(void * data, int64 ID) {
	m_Value = *((mat3X3*)data);
}

void C_Mat3X3插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

	fread(&m_Value, sizeof(mat3X3), 1, f);
}

void C_Mat3X3插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	fwrite(&m_Value, sizeof(mat3X3), 1, f);
}

PyObject * C_Mat3X3插座::f_输出_bl数据(PyObject* bl_data) {
	PyObject* data = 0;

	if (bl_data != (PyObject*)0xffffffff) {
		data = PyList_New(3);

		PyList_SetItem(data, 0, Py_BuildValue("(fff)", m_Value.row0.x, m_Value.row0.y, m_Value.row0.z));
		PyList_SetItem(data, 1, Py_BuildValue("(fff)", m_Value.row1.x, m_Value.row1.y, m_Value.row1.z));
		PyList_SetItem(data, 2, Py_BuildValue("(fff)", m_Value.row2.x, m_Value.row2.y, m_Value.row2.z));
	}
	else {
		data = PyList_New(4);

		//PyList_SetItem(data, 0, f_string转PyObject("3X3矩阵:"+m_identifier));
		PyList_SetItem(data, 1, Py_BuildValue("(fff)", m_Value.row0.x, m_Value.row0.y, m_Value.row0.z));
		PyList_SetItem(data, 2, Py_BuildValue("(fff)", m_Value.row1.x, m_Value.row1.y, m_Value.row1.z));
		PyList_SetItem(data, 3, Py_BuildValue("(fff)", m_Value.row2.x, m_Value.row2.y, m_Value.row2.z));
	}

	return data;
}












C_Matrix4X4插座::C_Matrix4X4插座(wstring name) :C_插座基类(name, E_值类型::e_Type_Mat4X4) {
	m_Value = f_mat44_identity();
}

C_Matrix4X4插座::~C_Matrix4X4插座() {
	C_插座基类::~C_插座基类();
}

void* C_Matrix4X4插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Mat4X4) {
			return dynamic_cast<C_Matrix4X4插座*>(socket)->f_getData(线程ID);
		}
		else if (socket->m_Type == E_值类型::e_Type_Mat4X4_1D) {
			this;
		}
		else if (socket->m_Type == E_值类型::e_Type_Mat4X4_Array1D) {
			auto& e = *DEF_Mat44_Array1D插座数据(socket, 0);
			if (e.size()) {
				m_Value = e[0];
			}
		}
	}
	return &m_Value;
}

void C_Matrix4X4插座::f_setData(void * data, int64 ID) {
	m_Value = *((Mat44f*)data);
}

void C_Matrix4X4插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

	fread(&m_Value, sizeof(Mat44f), 1, f);
}

void C_Matrix4X4插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	fwrite(&m_Value, sizeof(Mat44f), 1, f);
}

std::string C_Matrix4X4插座::f_构建变量代码(int32 loc) {
	std::string code;
	C_插座基类* socket = f_getLinkOutSocket(loc);
	if (socket) {
		if (socket->m_Type == e_Type_Mat4X4) {
			m_Code = socket->f_getCode(loc);
			return "";
		}

		m_Code = m_pNode->f_getName() + f_getName() + "_new_val_to_mat44";

		switch (socket->m_Type) {
		case e_Type_Mat4X4_1D: {
			code += "Mat44f " + m_Code + " = f_Mat44_array_formMat44Array(" + socket->f_getCode(loc) + ");\n";
			break;
		}
		//case e_Type_Vec4: {
		//	code += "S_Mat44Array* " + m_Code + " = f_Mat44_array_fromMat44_alloc_过度函数(" + socket->f_getCode() + ");\n";
		//	break;
		//}
		}

		//m_pNode->m_ReleaseCode += f_代码构建_销毁_Array(m_Code);
	}
	else {
		C_插座基类::f_构建变量代码(loc);
	}

	return code;
}

bool C_Matrix4X4插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
	case E_值类型::e_Type_Array:
	case E_值类型::e_Type_Mat4X4:
	case E_值类型::e_Type_Mat4X4_1D:
	case E_值类型::e_Type_Mat4X4_Array1D:
		return true;
	}
	return false;
}



PyObject* f_构建Mat4X4_Py数据(const C_Mat44& mats, C_Mat44* 转换坐标系, PyObject* bl_data) {
	PyObject* mat = 0;

	if (bl_data != (PyObject*)0xffffffff) {
		mat = PyTuple_New(4);
		
		if (转换坐标系) {
			C_Mat44 newMat = (*转换坐标系) * mats;
#pragma loop(on_vector)
			for (uint32 e = 0; e < 4; ++e) {
				PyTuple_SetItem(mat, e, Py_BuildValue("(ffff)",
					newMat.columns[e][0],
					newMat.columns[e][1],
					newMat.columns[e][2],
					newMat.columns[e][3]
				));
			}
		}
		else {
#pragma loop(on_vector)
			for (uint32 e = 0; e < 4; ++e) {
				//PyObject* r = PyTuple_New(4);
				PyTuple_SetItem(mat, e, Py_BuildValue("(ffff)",
					mats.columns[e][0],
					mats.columns[e][1],
					mats.columns[e][2],
					mats.columns[e][3]
				));
			}
		}

	}
	else {
		mat = PyList_New(4);

		if (转换坐标系) {
			C_Mat44 newMat = (*转换坐标系) * mats;
#pragma loop(on_vector)
			for (uint32 e = 0; e < 4; ++e) {
				PyList_SetItem(mat, e, Py_BuildValue("(ffff)",
					newMat.columns[e][0],
					newMat.columns[e][1],
					newMat.columns[e][2],
					newMat.columns[e][3]
				));
			}
		}
		else {
#pragma loop(on_vector)
			for (uint32 e = 0; e < 4; ++e) {
				PyList_SetItem(mat, e, Py_BuildValue("(ffff)",
					mats.columns[e][0],
					mats.columns[e][1],
					mats.columns[e][2],
					mats.columns[e][3]
				));
			}
		}
	}


	return mat;
}





C_Quat插座::C_Quat插座(std::wstring name) :C_插座基类(name, E_值类型::e_Type_Quat) {
	m_Value.x = 0;
	m_Value.y = 0;
	m_Value.z = 0;
	m_Value.w = 1;
}

C_Quat插座::~C_Quat插座() {
	C_插座基类::~C_插座基类();
}

void* C_Quat插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Quat) {
			return socket->f_getData(线程ID);
		}
	}
	return &m_Value;
}

void C_Quat插座::f_setData(void* data, int64 ID) {
	m_Value = *((Quat*)data);
}

void C_Quat插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

	fread(&m_Value, sizeof(Quat), 1, f);
}

void C_Quat插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	fwrite(&m_Value, sizeof(Quat), 1, f);
}








C_线段插座::C_线段插座(std::wstring name) :C_插座基类(name, E_值类型::e_插座Type_Line) {
	m_Value = {};
}

C_线段插座::~C_线段插座() {
	C_插座基类::~C_插座基类();
}

void* C_线段插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_插座Type_Line) {
			return socket->f_getData(线程ID);
		}
		else if (socket->m_Type == E_值类型::e_插座Type_Line_Array1D) {
			//return socket->f_getData(线程ID);
		}
		else if (socket->m_Type == E_值类型::e_Type_Vec3_Array1D) {
			auto& v3 = *DEF_Vec3_Array1D插座数据(socket);
			uint32 num = v3.size() / 2;

			if (num) {
				m_Value.begin = v3[0];
				m_Value.end = v3[1];
			}
		}
		else if (socket->m_Type == E_值类型::e_插座Type_引用数据) {
			auto* data = DEF_引用插座数据(socket, 0);
			switch (data->m_Type) {
			case E_值类型::e_插座Type_Line:
				return socket->f_getData(线程ID);

			case E_值类型::e_插座Type_Line_Array1D: {
				auto& v3 = *DEF_Vec3_Array1D插座数据(socket);
				uint32 num = v3.size() / 2;

				if (num) {
					m_Value.begin = v3[0];
					m_Value.end = v3[1];
				}
				break;
			}
			default:
				break;
			}
		}
		else {

		}
	}
	return &m_Value;
}

void C_线段插座::f_setData(void* data, int64 ID)
{
}

void C_线段插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
	f_读取文件(f, &m_Value);
}

void C_线段插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);
	f_写入文件(f, m_Value);
}














